/**
 * Created by Lcouncillor on 2020/5/13.
 */

function chkAll_onclick(obj){
//            var chkSorts = $('input[name="chkSort"]');
//            for ( var i=0; i<chkSorts.length; i++){
//                chkSorts[i].checked= obj.checked;
//            }
    var chks = $('input[type="checkbox"]');
    for ( var i=0; i<chks.length; i++){
        chks[i].checked= obj.checked;
    }
}

function chkSort_onclick(obj){

    //影响chkFruit（购物车订单）
    var chkSorts = $('input[name="chkSort"]');
    var index;
    for ( var i=0; i<chkSorts.length; i++){
        if( chkSorts[i] == obj){
            index = i+1;
        }
    }
    var chkFruits = $('input[name="chkFruit'+index+'"]');
    for ( var i=0; i<chkFruits.length; i++){
        chkFruits[i].checked = obj.checked;
    }

    //影响chkAll（全选）
    var chkAllChecked = isAll(obj);
    if (chkAllChecked == true){
        $('input[name="chkAll"]')[0].checked = true;
        $('input[name="chkAll"]')[1].checked = true;
    }else{
        $('input[name="chkAll"]')[0].checked = false;
        $('input[name="chkAll"]')[1].checked = false;
    }
}

function chkFruit_onclick(obj){
    var chkSortsChecked = isAll(obj);
    var chkSort = $('input[value="'+obj.name+'"]');
    if (chkSortsChecked == true){
        chkSort[0].checked = true;
    }else{
        chkSort[0].checked = false;
    }

    if ( obj.checked == false){
        $('input[name="chkAll"]')[0].checked = false;
        $('input[name="chkAll"]')[1].checked = false;
    }else{
        var sorts = $('input[name="chkSort"]');
        for (var i=0; i<sorts.length; i++){
            if (sorts[i].checked == false){
                return;
            }
        }
        $('input[name="chkAll"]')[0].checked = true;
        $('input[name="chkAll"]')[1].checked = true;
    }
}

//判断是否全选
function isAll(obj){
    var chks = $('input[name="'+obj.name+'"]');
    for ( var i=0; i<chks.length; i++){
        if( chks[i].checked == false){
            return false;
        }
    }

    return true;
}
